package algorithm.search;

/**
 * 判断一个字符串是否为Java关键字
 */
public class KeyWords {
    //关键字
    private static String[] keywords1 = {"private","protected","public","abstract","class","extends","final",
            "implements",
            "interface","native","new","static","strictfp","synchronized","transient","volatile","break","continue","return",
            "do","while","if","else","for","instanceof","switch","case","default","try","catch","throw","throws","import",
            "package","boolean","byte","char","double","float","int","long","short","null","true","false","super","this",
            "void","goto","const"};

    private static String[] keywords = {
            "abstract","boolean","break","byte","case","catch","char","class","continue","default","do","double","else",
            "extends","false","final","finally","float","for","if","implements","import",
            "instanceof","int","interface","long","native","new","null","package","private",
            "protected","public","return","short","static","super","switch","synchronized","this","throw","transient"
            ,"true","try","void"
    };

    //索引表
    private static IndexItem index[] = {
            new IndexItem("a",0),
            new IndexItem("b",1),
            new IndexItem("c",4),
            new IndexItem("d",9),
            new IndexItem("e",12),
            new IndexItem("f",14),
            new IndexItem("i",19),
            new IndexItem("I",25),
            new IndexItem("n",26),
            new IndexItem("p",29),
            new IndexItem("r",33),
            new IndexItem("s",34),
            new IndexItem("t",39),
            new IndexItem("v",45),
            new IndexItem("w",46)
    };

    //内部类，私有成员
    private static class IndexItem implements Comparable<IndexItem> {
        String first;//关键字的首字母
        int start;//块起始下标
        public IndexItem (String first,int i) {
            this.first = first;
            start = i;
        }
        public IndexItem (String first) {
            this(first,-1);
        }
        public String toString() {
            return this.first;
        }

        //约定两个索引项比较大小的规则，实现Comparable接口
        public int compareTo(IndexItem item){
            return this.first.compareTo(item.first);//按首字母比较大小
        }
    }
    //判断Str是否为java关键字
    public static boolean isKeyword(String str) {
        if(str!=null){
            IndexItem item = new IndexItem(str.substring(0,1));//首字母对应的索引项
            System.out.println("index:"+index+" item:"+item);
            int pos = BinarySearch.binarySearch(index,item);
            System.out.println("pos:"+pos);
            int left = index[pos].start;
            int right = index[pos+1].start-1;
            int find = BinarySearch.binarySearch(keywords,str,left,right);
            return find >= 0;
        }
        return false;
    }

    public static void main(String[] args) {
        //todo 该程序有异常
        String str = "final";
        System.out.println(str+(isKeyword(str)?"":"不")+"关键字");
    }
}
